package com.yunfeng.supercall.activity;

import org.json.JSONObject;

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentStatePagerAdapter;
import android.support.v4.view.ViewPager;
import android.support.v4.view.ViewPager.OnPageChangeListener;
import android.view.View;
import android.widget.RadioGroup;
import android.widget.RadioGroup.OnCheckedChangeListener;

import com.yunfeng.supercall.R;
import com.yunfeng.supercall.fragment.RegStep2EnterPriseFragment;
import com.yunfeng.supercall.fragment.RegStep2PersonalFragment;

/**
 * @author 洋
 * 
 */
public class RegActivity extends BaseActivity {

	private RadioGroup mRadioGroup;
	private ViewPager mViewPager;
	private int mType;

	@Override
	public void onClick(View v) {
		// TODO Auto-generated method stub
		super.onClick(v);
		switch (v.getId()) {
		case R.id.right_text:
			next(v);
			break;

		default:
			break;
		}
	}

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_reg_step1);
		mType = getIntent().getIntExtra(KEY_DATA, 0);
		mViewPager = findView(R.id.view_pager);
		mRadioGroup = findView(R.id.rg);
		mRadioGroup.check(R.id.rb1);
		mRadioGroup.setOnCheckedChangeListener(new OnCheckedChangeListener() {
			@Override
			public void onCheckedChanged(RadioGroup group, int checkedId) {
				// TODO Auto-generated method stub
				switch (checkedId) {
				case R.id.rb1:
					mViewPager.setCurrentItem(0);
					break;
				case R.id.rb2:
					mViewPager.setCurrentItem(1);
					break;
				}
			}
		});

		mViewPager
				.setAdapter(new ViewPagerAdapter(getSupportFragmentManager()));
		mViewPager.setOnPageChangeListener(new OnPageChangeListener() {

			@Override
			public void onPageSelected(int arg0) {
				// TODO Auto-generated method stub
				mRadioGroup.check(arg0 == 0 ? R.id.rb1 : R.id.rb2);
			}

			@Override
			public void onPageScrolled(int arg0, float arg1, int arg2) {
				// TODO Auto-generated method stub
			}

			@Override
			public void onPageScrollStateChanged(int arg0) {
				// TODO Auto-generated method stub

			}
		});
	}

	public void next(View v) {
		Fragment fragment = (Fragment) mViewPager.getAdapter().instantiateItem(
				mViewPager, mViewPager.getCurrentItem());
		if (fragment instanceof ParamsGenerator) {
			JSONObject json = ((ParamsGenerator) fragment).generateParams();
			// if (json != null) {
			// startActivity(new Intent(this, RegStep2Activity.class)
			// .putExtra("data", json.toString()));
			// }
		}
	}

	public static interface ParamsGenerator {
		JSONObject generateParams();

		void emptyParams();
	}

	class ViewPagerAdapter extends FragmentStatePagerAdapter {

		public ViewPagerAdapter(FragmentManager fm) {
			super(fm);
			// TODO Auto-ge4nerated constructor stub
		}

		@Override
		public Fragment getItem(int arg0) {
			// TODO Auto-generated method stub
			if (mType == 0) {
				return new RegStep2PersonalFragment();
			}
			return new RegStep2EnterPriseFragment();
		}

		@Override
		public int getCount() {
			// TODO Auto-generated method stub
			return 1;
		}

	}
}
